package com.zhengdongzulin.zuke.app.main.mine.doorlock

import android.content.Intent
import android.text.TextUtils
import androidx.lifecycle.Observer
import com.google.gson.Gson
import com.lxj.androidktx.core.*
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.ChangeHomeData
import com.zhengdongzulin.zuke.app.data.ContractListData
import com.zhengdongzulin.zuke.app.main.mine.bill.ZKBillActivity
import com.zhengdongzulin.zuke.base.*
import com.zhengdongzulin.zuke.databinding.ZkActivityLockBinding
import com.zhengdongzulin.zuke.ext.setColor
import com.zhengdongzulin.zuke.ext.start

/**
 * 智能门锁
 */
class ZKDoorLockActivity : BaseActivity<DoorLockVm, ZkActivityLockBinding>() {

    var mData = mutableListOf<ContractListData>()

    override fun getLayoutResId(): Int = R.layout.zk_activity_lock

    override fun initVM(): DoorLockVm = getVM(DoorLockVm::class.java)

    override fun initView() {
        if (!TextUtils.isEmpty(sp().getString("change_home", ""))) {
            val id =
                Gson().fromJson(sp().getString("change_home", ""), ChangeHomeData::class.java).houseId
            mViewModel.get_lock_detail(id)
        }
    }

    override fun initData() {
    }

    override fun initListener() {
        mView.changeLock.setOnClickListener {
            mViewModel.get_user_arrears(
                Gson().fromJson(
                    sp().getString("change_home", ""),
                    ChangeHomeData::class.java
                ).houseId
            )
        }
    }


    override fun startObserve() {
        mViewModel.get_lock_detail.observe(this, androidx.lifecycle.Observer {
            if (it.bindStatus == 0) {//是否绑定智能门锁 1绑定 0未绑定
                mView.img.setImageResource(R.mipmap.lock_no_install)
                setTitleContent("门锁未绑定")
                mView.llCount.gone()
                mView.changeLock.gone()
                mView.tvErr.visible()
            } else {
                mView.llCount.visible()
                mView.changeLock.visible()
                mView.tvErr.gone()
                if (it.quantityStatus == 1) {//电量状态 1 正常 0 低电量
                    mView.tvCount.setColor(R.color.fcd261)
                    mView.img.setImageResource(R.mipmap.lock_en)
                } else {
                    mView.img.setImageResource(R.mipmap.lock_down)
                    mView.tvCount.setColor(R.color.f13e0d)
                }
                mView.tvCount.text = "${it.electricQuantity}"
                setTitleContent(it.houseAddress)
            }
        })

        mViewModel.get_lock_detail.state.observe(this, getObserver(mViewModel.get_lock_detail))
        mViewModel.get_user_arrears.observe(this, Observer {
            if (!it.isNullOrEmpty() && it == "1") {//0可以修改密码继续走后续流程	1已经有逾期的跳转到账单列表
                MaterialDialog.Builder(this)
                    .content("您已欠租,门锁已经冻结,请在我的账单中进行缴费")
                    .negativeText("关闭")
                    .positiveText("去缴费")
                    .positiveColor(color(R.color.color_1273f7))
                    .contentColor(color(R.color.black_66))
                    .negativeColor(color(R.color.gray_99))
                    .cancelable(false)
                    .onNegative { dialog, which ->
                        dialog.dismiss()
                    }
                    .onPositive { dialog, which ->
                        dialog.dismiss()
                        Intent().start(this, ZKBillActivity::class.java)
                    }
                    .show()
            } else {
                Intent().start(this, ZKChangeLockPwdActivity::class.java)
            }
        })
        mViewModel.get_user_arrears.state.observe(this, getObserver(mViewModel.get_user_arrears))
    }

}